$(function () {
    $('#Commonbottom').load('../assets/public/html/PublicBottom.html')

    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

    function errorWindows(text1, text2) {
        toastr.error(`${text2}`, `${text1}`)
    }

    function successWindows(text1, text2) {
        toastr.success(`${text2}`, `${text1}`)
    }

    //初始化请求数据
    function initializationAudit() {
        let personnelAudit = Interceptor('/personnelAudit', 'post')
        return personnelAudit
    }

    let personnelAudit = initializationAudit()
    pagePagination(personnelAudit)

    $('.examine li').click(function () {
        const states = $(this).attr('state')
        $(this).children().addClass('fbtn')
        $(this).siblings().children().removeClass('fbtn')
        if (states == undefined) {
            pageCom(initializationAudit())
            pagePagination(initializationAudit())
        } else {
            changeStates(states)
        }
    })

    //切换状态渲染的数据
    function changeStates(stateId) {
        const personnelAudit = Interceptor('/personnelAudit', 'post', {
            'state': stateId
        })

        pageCom(personnelAudit)
        pagePagination(personnelAudit)
    }

    //渲染数据
    function pageCom(res) {
        $('.examine_content ul').html('')
        $('.examine h3 span').text('(' + res.data.total + ')')
        //判断内容是否为空
        if (res.data.total != 0) {
            $('#examieComUl').removeClass('nullUl')
        } else {
            $('#examieComUl').addClass('nullUl').text('暂无内容')
        }
        //将用户投档的数据遍历出来
        for (let i = 0; i < res.data.data.length; i++) {
            // let area = res.data.data[i].company_area;
            // area = area.slice(0, 6)
            let age = res.data.data[i].age;
            age = Math.trunc(age);
            if (res.data.data[i]) {
                const pageCom = `
            <li class="examine_contetnli">
                <div class="user_msg">
                    <div class="user_msg1">
                        <div><img src="${res.data.data[i].header}" alt=""></div>
                        
                        <span>${res.data.data[i].username}</span>
                    </div>
                    <div class="user_msg2">
                        <h4><img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/add.png" alt="" style="margin-right: 4px;">基本信息</h4>

                        <div> 年龄：<span class="age">${age}</span>
                            工作时间:<span class="work_time">${res.data.data[i].work_alltime}</span>
                            目前位于: <span class="area_postion">${res.data.data[i].workplace}</span>

                            目前状态：<span class="now_status"> ${res.data.data[i].status}</span><br>
                            联系电话：<span class="user_phone">${res.data.data[i].phone}</span>
                            邮箱： <span class="user_email">${res.data.data[i].email}</span>
                        </div>

                    </div>
                    <div class="user_msg3">
                        <h4><img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/fabulous.png" alt="" style="margin-right: 4px;"> 个人优势</h4>
                        <p>${res.data.data[i].advantage}</p>
                    </div>

                </div>
                <div class="user_status">
                    <ul value="${res.data.data[i].state}">
                        <li >退档</li>
                        <li>待审核</li>
                        <li>待面试</li>
                        <li>已面试</li>
                        <li>聘用</li>
                    </ul>


                    <div class="btn_box">
<!--                        <div class="btn_con2 employ" onclick="hire($(this))">聘用</div>-->
                        <button class="btn_con2 interview" onclick="hire($(this))">面试</button>
                        <button class="btn_con2 employ" onclick="backOff($(this))">退档</button>
                    </div>
                </div>
            </li>`

                $('#examieComUl').append(pageCom)
            }
        }

        statusCurrent()
    }

    pageCom(personnelAudit)

    //更新状态条的状态
    function statusCurrent() {
        const iptUl = $('.user_status ul');

        for (let i = 0; i < iptUl.length; i++) {
            const value = $(iptUl[i]).attr('value')
            const allBtn = $(iptUl[i]).parent().children('div').children();
            const hirekBtn = $(iptUl[i]).parent().children('div').children().eq(0);
            const backBtn = $(iptUl[i]).parent().children('div').children().eq(1);
            $(iptUl[i]).children().eq(value - 1).addClass('status_current').siblings().removeClass('status_current')

            switch (parseInt(value)) {
                case 1:
                    allBtn.prop('disabled', true).addClass('back')
                    break
                case 2:
                    break
                case 3:
                    hirekBtn.text('已面试')
                    break
                case 4:
                    hirekBtn.text('聘用')
                    break
                case 5:
                    hirekBtn.text('已聘用').prop('disabled', true).addClass('back')
                    backBtn.prop('disabled', true).addClass('back')
                    break
            }
        }
    }

    statusCurrent()

    //分页
    function pagePagination(res) {
        $('.pageBox').pagination({
            current: 1,
            showData: 8,
            pageCount: res.data.last_page,
            activeCls: 'page_itemActive',
            callback: function (index) {
                pageIndex = index.getCurrent();
                // pageCom(pageIndex)
                let states = ''
                for (let i = 0; i < $('.examine li').length; i++) {
                    if ($($('.examine li').eq(i)).children().hasClass('fbtn')) {
                        states = $($('.examine li').eq(i)).attr('state')
                    }
                }
                let personnelAudit = Interceptor('/personnelAudit', 'post', {
                    'state': states,
                    'currPage': pageIndex
                })

                pageCom(personnelAudit)
            }
        })
    }

    //面试按钮
    hire = function (dom) {
        const val = dom.parents('.user_status').children('ul').attr('value')
        if (val == 0) {
            errorWindows('当前职位已退档', '')
        } else {

            const phone = dom.parents('.examine_contetnli').children('.user_msg').children('.user_msg2').children('div').children('.user_phone').text()
            const peUpState = Interceptor('/peUpState', 'post', {
                'applyPhone': phone,
                'commit': true
            })
            const {code, message} = peUpState

            if (code != 666) {
                errorWindows(message, '')
            } else {
                successWindows(message, '')
                pageCom(initializationAudit())
            }
        }

    }

    //退档
    backOff = function (dom) {
        const val = dom.parents('.user_status').children('ul').attr('value')
        if (val == 0) {
            errorWindows('当前职位已退档', '')
        } else {
            const phone = dom.parents('.examine_contetnli').children('.user_msg').children('.user_msg2').children('div').children('.user_phone').text()
            const peUpState = Interceptor('/peUpState', 'post', {
                'applyPhone': phone,
                'commit': 1
            })
            const {code, message} = peUpState

            if (code != 666) {
                errorWindows(message, '')
            } else {
                successWindows(message, '')
                pageCom(initializationAudit())
            }
        }

    }
})
